package com.cs5103.calendar;

import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CalendarFragment extends Fragment {
	private TextView mCalendarMonthHeader;
	private GridView mCalendarDaysOfWeekHeader;
	private GridView mCalendarGrid;
	private ArrayList<CalendarCell> mCells;
	private String mCalendarHeader;
	
	private CalendarGridAdapter gridAdapter;
	private DaysOfWeekAdapter headerAdapter;
	
	public CalendarFragment() {
		Bundle args = new Bundle();
		this.setArguments(args);
		CalendarData.get(getActivity()).setDateSelected(Calendar.getInstance());
		CalendarData.get(getActivity()).setDateCurrentView(Calendar.getInstance());
		initializeCells();
		initializeHeader();
	}
	
//	static CalendarFragment initCalendarFragment() {
//		CalendarFragment fragment = new CalendarFragment();
//		Bundle args = new Bundle();
//		fragment.setArguments(args);
//		return fragment;
//	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		mCells = CalendarData.get(getActivity()).getCells();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup parent, 
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.calendar_fragment, parent, false);
		
		mCalendarMonthHeader = (TextView)v.findViewById(R.id.calendar_month_header);
		mCalendarMonthHeader.setText(mCalendarHeader);
		
		mCalendarDaysOfWeekHeader = (GridView)v.findViewById(R.id.calendar_daysofweek_header);
		headerAdapter = new DaysOfWeekAdapter(CalendarData.get(getActivity())
				.getDaysOfWeek_Abbr());
		mCalendarDaysOfWeekHeader.setBackgroundColor(getResources().getColor(R.color.black));
		mCalendarDaysOfWeekHeader.setAdapter(headerAdapter);
		
		mCalendarGrid = (GridView)v.findViewById(R.id.calendar_grid);
		gridAdapter = new CalendarGridAdapter(mCells);
		mCalendarGrid.setBackgroundColor(getResources().getColor(R.color.black));
		mCalendarGrid.setAdapter(gridAdapter);
		
		return v;
	}

	private void initializeCells() {
		Calendar mDateStart;
		CalendarCell mCell;
		int mCurrentMonth = CalendarData.get(getActivity()).getDateCurrentView().get(Calendar.MONTH);
		int mSelectedYear = CalendarData.get(getActivity()).getDateSelected().get(Calendar.YEAR);
		int mSelectedMonth = CalendarData.get(getActivity()).getDateSelected().get(Calendar.MONTH);
		int mSelectedDay = CalendarData.get(getActivity()).getDateSelected().get(Calendar.DAY_OF_MONTH);
		int mNumberOfCells = CalendarData.get(getActivity()).getCellExtent();
		if (mCells.size() > 0)
			mCells.clear();
		mDateStart = (Calendar)CalendarData.get(getActivity()).getDateCurrentView().clone();
		mDateStart.set(Calendar.DAY_OF_MONTH, 1);
		int i = mDateStart.get(Calendar.DAY_OF_WEEK);
		mDateStart.add(Calendar.DAY_OF_MONTH, -(i - 1));
		for (int j = 0; j < mNumberOfCells; j++) {
			mCell = new CalendarCell(mDateStart);
			if (mCell.getCellDate().get(Calendar.MONTH) == mCurrentMonth)
				mCell.setCurrentMonth(true);
			else
				mCell.setCurrentMonth(false);
			if ((mCell.getCellDate().get(Calendar.YEAR) == mSelectedYear) & (mCell.getCellDate().get(Calendar.MONTH) == mSelectedMonth)
						& (mCell.getCellDate().get(Calendar.DAY_OF_MONTH) == mSelectedDay))
				mCell.setSelected(true);
			else
				mCell.setSelected(false);
			mCells.add(mCell); 
			mDateStart.add(Calendar.DAY_OF_MONTH, 1);
		}
	}

	private void initializeHeader() {
		String mYearString, mMonthString, mDayString;
		int mYearInt, mMonthInt, mDayInt;
		
		mYearInt = CalendarData.get(getActivity()).getDateSelected().get(Calendar.YEAR);
		mYearString = Integer.toString(mYearInt);
		mMonthInt = CalendarData.get(getActivity()).getDateSelected().get(Calendar.MONTH);
		mMonthString = CalendarData.get(getActivity()).getMonths()[mMonthInt];
		mDayInt = CalendarData.get(getActivity()).getDateSelected().get(Calendar.DAY_OF_MONTH);
		mDayString = Integer.toString(mDayInt);
		mCalendarHeader = mDayString + " " + mMonthString + " " + mYearString;
	}
	

	/*
	// Array adapter to add days of week to Calendar header GridView in calendar_fragment.xml
	*/
	
	private class DaysOfWeekAdapter extends ArrayAdapter<String> {
		
		public DaysOfWeekAdapter(String[] mDaysOfWeek) {
			super(getActivity(), 0, mDaysOfWeek);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater()
						.inflate(R.layout.calendar_header_grid_layout, null);
			}
			
			String mDay = getItem(position);
			TextView mDayView = (TextView)convertView.findViewById(R.id.calendar_header_grid_text);
			mDayView.setGravity(Gravity.CENTER);
			mDayView.setBackgroundColor(getResources().getColor(R.color.white));
			mDayView.setTypeface(null, Typeface.BOLD);
			mDayView.setText(mDay);
			
			return convertView;
		}
	}
	
	/* Adapter to handle the grid portion of a calendar inflated from calendar_grid in calendar_fragment
	 * Sets the data and layout for each cell in the grid
	 * Adjusts the cell attributes for user preferences
	 * Implements onClick functionality to select a specific day
	 * 
	 * Adjusts the calendar header to the selected day
	 */
	
	private class CalendarGridAdapter extends ArrayAdapter<CalendarCell> {

		public CalendarGridAdapter(ArrayList<CalendarCell> mCells) {
			super(getActivity(), 0, mCells);
			// TODO Auto-generated constructor stub
		}

		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater()
						.inflate(R.layout.calendar_cell_layout, null);
			}			
			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					CalendarCell mCell;
					mCell = getItem(position);
					CalendarData.get(getActivity()).setDateSelected(mCell.getCellDate());
					initializeCells();
					initializeHeader();
					mCalendarMonthHeader.setText(mCalendarHeader);
					notifyDataSetChanged();
				}
			});

			CalendarCell mCell = getItem(position);
			if (mCell.isSelected())
				convertView.setBackgroundColor(getResources().getColor(R.color.red));
			else
				if (mCell.isWeekend())
					convertView.setBackgroundColor(getResources().getColor(R.color.Bisque));				
				else
					convertView.setBackgroundColor(getResources().getColor(R.color.white));

			TextView mCellDateView = (TextView)convertView.findViewById(R.id.calendar_cell_date);
			if (mCell.isCurrentMonth())
				mCellDateView.setTextColor(getResources().getColor(R.color.black));
			else
				mCellDateView.setTextColor(getResources().getColor(R.color.LightGrey));
			mCellDateView.setText(mCell.getCellDateString());
			TextView mCellEventView = (TextView)convertView.findViewById(R.id.calendar_cell_event);
			mCellEventView.setText("1");
			
			return convertView;
		}
	}
}
